package com.larry.tester;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class MainTester {

	private static String fileName = "com/larry/file/Data.txt";

	public static void main(String[] args) throws Exception {
		MainTester tester = new MainTester();

		System.out.println();

		// tester.test4();
		// tester.test3();
		// tester.test2();
		tester.test5();
	}

	private void test6() throws Exception {
		Properties prop = new Properties();

		try {
			// load a properties file
			prop.load(new FileInputStream("config.properties"));

			// get the property value and print it out
			System.out.println(prop.getProperty("search.table"));
			System.out.println(prop.getProperty("search.start.date"));
			System.out.println(prop.getProperty("search.end.date"));

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	private void test5() throws Exception {
		Properties prop = new Properties();

		try {
			// set the properties value
			prop.setProperty("search.table", "rm_item_detail@peu_ers_link");
			prop.setProperty("search.start.date", "2013/01/01");
			prop.setProperty("search.end.date", "2013/01/31");

			// save properties to project root folder
			prop.store(new FileOutputStream("config.properties"), null);

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	private void test4() throws Exception {
		String s3 = "sn-3";
		String path = Thread.currentThread().getContextClassLoader().getResource("").getFile();
		File file = new File(path.replace("/bin/", "/src/") + fileName);
		BufferedWriter writer = new BufferedWriter(new FileWriter(file));
		BufferedWriter out = new BufferedWriter(writer);
		out.write(s3 + "\n");
		out.close();
		writer.close();

	}

	private void test3() throws Exception {
		String s3 = "sn-3";
		String path = Thread.currentThread().getContextClassLoader().getResource("").getFile();
		File file = new File(path.replace("/bin/", "/src/") + fileName);
		// BufferedWriter writer = new BufferedWriter(new FileWriter(file));

		List<String> result = new ArrayList<String>();

		FileInputStream fis = new FileInputStream(file);
		BufferedInputStream bis = new BufferedInputStream(fis);
		DataInputStream dis = new DataInputStream(bis);
		while (dis.available() != 0) {
			String line = dis.readLine();
			if (line.equalsIgnoreCase(s3)) {
				continue;
			}
			result.add(line);
		}
		dis.close();
		bis.close();
		fis.close();

		BufferedWriter writer = new BufferedWriter(new FileWriter(file));
		BufferedWriter out = new BufferedWriter(writer);
		for (String string : result) {
			out.write(string + "\n");
		}
		out.close();
		writer.close();
	}

	private void test2() throws Exception {
		List<String> result = new ArrayList<String>();
		result.add("sn-1");
		result.add("sn-3");
		result.add("sn-2");
		result.add("sn-4");

		String path = Thread.currentThread().getContextClassLoader().getResource("").getFile();
		FileWriter fileWriter = new FileWriter(path.replace("/bin/", "/src/") + fileName);
		System.out.println(path + fileName);
		BufferedWriter out = new BufferedWriter(fileWriter);
		for (String string : result) {
			out.write(string + "\n");
		}
		out.close();
		fileWriter.close();
	}

	private void test1() throws Exception {
		String path = Thread.currentThread().getContextClassLoader().getResource("").getFile();
		System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getFile());
		File file = new File(path + fileName);
		System.out.println(file);
		FileInputStream fis = new FileInputStream(file);
		BufferedInputStream bis = new BufferedInputStream(fis);
		DataInputStream dis = new DataInputStream(bis);
		while (dis.available() != 0) {
			String line = dis.readLine();
			System.out.println(line);
		}
	}
}
